
Partial Class Admin_TempSupportBrokerPermission
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SetupJavaScript()
        ClearStatus()

        If UserAuthorization.CurrentUser.RoleId = Cdm.DataCore.UserSecurity.Role.RoleType.SupportBrokerAdmin Then
            Me.RolePermissionsSubNavControl1.Visible = False
        End If

        If Not Me.IsPostBack Then
            BindAgencyDropDown()
            BindData()
            Me.StartDateTextBox.Text = Now.ToShortDateString()
            Me.EndDateTextBox.Text = DateAdd(DateInterval.Day, 7, Now).ToShortDateString()
        End If
    End Sub

    Private Sub SetupJavaScript()
        EntityController.JavaScriptSetFixedHeight(30)
        EntityController.JavaScriptAddChildDiv(Me.TempPermissionScrollingGridView.DivClientId, 160)
    End Sub

    Private Sub ClearStatus()
        SetStatus("")

    End Sub
    Private Sub SetStatus(ByVal message As String)
        Dim mymaster As ASP.admin_admin_master = CType(Me.Master, ASP.admin_admin_master)
        mymaster.SetStatus(message)
    End Sub

    Private Sub BindAgencyDropDown()
        Dim agencyList As System.Collections.Generic.List(Of Cdm.DataCore.Entities.SupportBrokerAgency)
        Dim isSBAUser As Boolean = False

        
        If UserAuthorization.CurrentUser.RoleId = Cdm.DataCore.UserSecurity.Role.RoleType.SupportBrokerAdmin Then
            agencyList = Cdm.DataCore.Entities.SupportBrokerAgency.ListByUser(UserAuthorization.CurrentUser)
            isSBAUser = True
            Dim agencyId As Integer = agencyList(0).SupportBrokerAgencyId
        Else
            agencyList = Cdm.DataCore.Entities.SupportBrokerAgency.ListAll()
        End If

        Me.AgencyDropDownList.DataSource = agencyList
        Me.AgencyDropDownList.DataTextField = "AgencyName"
        Me.AgencyDropDownList.DataValueField = "SupportBrokerAgencyId"
        Me.AgencyDropDownList.DataBind()
        If Not isSBAUser Or agencyList.Count = 0 Then
            Me.AgencyDropDownList.Items.Insert(0, New ListItem("[None]", "0"))
        End If


    End Sub

    Private Sub BindData()

        BindGrid()
        BindSupportBrokerDropDown(Me.SupportBrokerDropDownList)
        BindSupportBrokerDropDown(Me.PermissionSupportBrokerDropDownList)
        
    End Sub

    Private Sub BindSupportBrokerDropDown(ByVal ddl As DropDownList)
        Dim agencyId As Integer = CInt(AgencyDropDownList.SelectedValue)
        Dim sbList As System.Collections.Generic.IList(Of Cdm.DataCore.Entities.SupportBroker) = Cdm.DataCore.Entities.SupportBroker.ListByAgency(agencyId, "0", "...")
        ddl.DataSource = sbList
        ddl.DataTextField = "DisplayLastNameFirst"
        ddl.DataValueField = "SupportBrokerId"
        ddl.DataBind()
    End Sub

    Private Sub BindGrid()
        Dim agencyId As Integer = CInt(AgencyDropDownList.SelectedValue)
        Me.TempPermissionScrollingGridView.DataSource = Cdm.DataCore.UserSecurity.TempSBPermission.ListByAgency(agencyId, Me.HideExpiredCheckBox.Checked)
        Me.TempPermissionScrollingGridView.DataBind()

    End Sub

    Protected Sub SubmitAgencyButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitAgencyButton.Click
        BindData()
    End Sub

    Protected Sub AddButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddButton.Click
        SetDetailsVisible(True)
    End Sub

    Private Sub SetDetailsVisible(ByVal isDetailVisible As Boolean)
        Me.TempPermissionScrollingGridView.Visible = Not isDetailVisible
        Me.AddPermissionPanel.Visible = isDetailVisible

        Me.SaveButton.Visible = isDetailVisible
        Me.CancelButton.Visible = isDetailVisible
        Me.AddButton.Visible = Not isDetailVisible
    End Sub

    Protected Sub CancelButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CancelButton.Click
        SetDetailsVisible(False)
        SetStatus("Action cancelled.")
    End Sub

    Protected Sub SaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveButton.Click
        Dim tmp As New Cdm.DataCore.UserSecurity.TempSBPermission()
        tmp.PermissionedSupportBrokerId = CInt(Me.PermissionSupportBrokerDropDownList.SelectedValue)
        tmp.SupportBrokerId = CInt(Me.SupportBrokerDropDownList.SelectedValue)
        tmp.StartDate = CDate(Me.StartDateTextBox.Text)
        tmp.EndDate = CDate(Me.EndDateTextBox.Text)
        tmp.LastModified = UserAuthorization.CurrentUser.LogonId
        tmp.DateModified = Now
        tmp.Persist()
        SetDetailsVisible(False)
        SetStatus("New Permission has been set.")
        BindData()
    End Sub

    Protected Sub TempPermissionScrollingGridView_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles TempPermissionScrollingGridView.RowCancelingEdit
        TempPermissionScrollingGridView.EditIndex = -1
        SetStatus("Edit Cancelled.")
        BindData()
    End Sub

    Protected Sub TempPermissionScrollingGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles TempPermissionScrollingGridView.RowDataBound
        Dim currentDataItem As Cdm.DataCore.UserSecurity.TempSBPermission = CType(e.Row.DataItem, Cdm.DataCore.UserSecurity.TempSBPermission)

        If e.Row.RowIndex = 0 Then
            AccessibilityController.AddImageToSortableGridHeaders(TempPermissionScrollingGridView)
        End If

        If Not e.Row.FindControl("SupportBrokerDropDownList") Is Nothing Then
            Dim sbDDL As DropDownList = CType(e.Row.FindControl("SupportBrokerDropDownList"), DropDownList)
            BindSupportBrokerDropDown(sbDDL)
            sbDDL.SelectedValue = currentDataItem.SupportBrokerId.ToString()
        End If

        If Not e.Row.FindControl("PermissionedSupportBrokerDropDownList") Is Nothing Then
            Dim permSBDDL As DropDownList = CType(e.Row.FindControl("PermissionedSupportBrokerDropDownList"), DropDownList)
            BindSupportBrokerDropDown(permSBDDL)
            permSBDDL.SelectedValue = currentDataItem.PermissionedSupportBrokerId.ToString()
        End If
    End Sub

    Protected Sub TempPermissionScrollingGridView_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles TempPermissionScrollingGridView.RowDeleting
        Dim deletePermission As Cdm.DataCore.UserSecurity.TempSBPermission
        Dim permissionId As Integer = Me.TempPermissionScrollingGridView.DataKeys(e.RowIndex).Value
        deletePermission = Cdm.DataCore.UserSecurity.TempSBPermission.Retrieve(permissionId)
        deletePermission.Remove()
        SetStatus("Permission has been deleted.")
        BindData()
    End Sub

    Protected Sub TempPermissionScrollingGridView_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles TempPermissionScrollingGridView.RowEditing
        TempPermissionScrollingGridView.EditIndex = e.NewEditIndex
        BindData()
    End Sub



    Protected Sub TempPermissionScrollingGridView_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles TempPermissionScrollingGridView.RowUpdating
        Dim bCancel As Boolean = False
        Dim editPermission As Cdm.DataCore.UserSecurity.TempSBPermission
        Dim permissionId As Integer = Me.TempPermissionScrollingGridView.DataKeys(e.RowIndex).Value
        editPermission = Cdm.DataCore.UserSecurity.TempSBPermission.Retrieve(permissionId)
        Dim currRow As GridViewRow = TempPermissionScrollingGridView.Rows(e.RowIndex)
        Dim startDateTextBox As TextBox = CType(currRow.FindControl("StartDateTextBox"), TextBox)
        Dim endDateTextBox As TextBox = CType(currRow.FindControl("EndDateTextBox"), TextBox)
        Dim sbDropDown As DropDownList = CType(currRow.FindControl("SupportBrokerDropDownList"), DropDownList)
        Dim permDropDown As DropDownList = CType(currRow.FindControl("PermissionedSupportBrokerDropDownList"), DropDownList)

        If Not IsDate(startDateTextBox.Text) Then
            SetStatus("Start Date is not in date format.")
            bCancel = True
        ElseIf Not IsDate(endDateTextBox.Text) Then
            SetStatus("End Date is not in date format.")
            bCancel = True
        End If

        If bCancel = True Then
            e.Cancel = True
        Else
            editPermission.StartDate = CDate(startDateTextBox.Text)
            editPermission.EndDate = CDate(endDateTextBox.Text)
            editPermission.SupportBrokerId = CInt(sbDropDown.SelectedValue)
            editPermission.PermissionedSupportBrokerId = CInt(permDropDown.SelectedValue)
            editPermission.LastModified = UserAuthorization.CurrentUser.LogonId
            editPermission.DateModified = Now
            editPermission.Persist()
            SetStatus("Permission successfully edited.")
            Me.TempPermissionScrollingGridView.EditIndex = -1
            BindData()
        End If


    End Sub
End Class
